package net.wangds.imagecomp.comp;

import org.springframework.stereotype.Component;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * 图片工具组件.
 * <p>利用此工具可以对图片进行缩放，裁剪.</p>
 */
@Component
public class BufferedImageComp {
    /**
     * 保持透明度度的BufferedImage.
     * @param img 原始图片.
     * @return 图片.
     */
    public static BufferedImage toTranslucentImage(BufferedImage img){
        BufferedImage res = img.createGraphics().getDeviceConfiguration().createCompatibleImage(img.getWidth(null), img.getHeight(null), Transparency.TRANSLUCENT);
        DrawOperate.draw(res.getGraphics(), (g)->g.drawImage(img, 0,0,null));
        return res;
    }

    /**
     * 截取正方形.
     * @param img 图片.
     * @return 正方形图片.
     */
    public static BufferedImage cutSquare(BufferedImage img){
        if(img==null){
            return null;
        }
        int width = img.getWidth();
        int height = img.getHeight();
        int size = Math.min(width, height);
        if(size<=0){
            size = 1;
        }

        BufferedImage res = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);

        int top = (height-size)/2;
        int left = (width-size)/2;
        int s = size;
        DrawOperate.draw(res.getGraphics(), (g)-> g.drawImage(img, 0,0,s, s,left,top,s,s, null));

        return res;

    }

    /**
     * 缩放图片到正方形.
     * <p>缩放后的边长为原始图片的长、宽中较短的。</p>
     * @param img 原始图片.
     * @return 缩放后的图片.
     */
    public static BufferedImage scale2Square(BufferedImage img){
        if(img==null){
            return null;
        }
        int width = img.getWidth();
        int height = img.getHeight();
        int size = Math.min(width, height);
        if(size<=0){
            size = 1;
        }

        BufferedImage res = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);

        int s = size;
        DrawOperate.draw(res.getGraphics(), (g)-> g.drawImage(img, 0,0,s, s,0,0,width,height, null));

        return res;
    }

    /**
     * 缩放原始图片中的一部分到指定大小的新图片.
     * @param ori 原始图片.
     * @param distWidth 新图片的宽度.
     * @param distHeight 新图片的高度.
     * @param oriLeft 截取原始图片的范围最左侧坐标.
     * @param oriTop 截取原始图片的范围上部侧坐标.
     * @param oriWidth 截取原始图片的范围宽度.
     * @param oriHeight 截取原始图片的范围高度.
     * @return 部分缩放后的图片.
     */
    public static BufferedImage scale(BufferedImage ori, int distWidth, int distHeight, int oriLeft, int oriTop, int oriWidth, int oriHeight){
        BufferedImage res = ori.createGraphics().getDeviceConfiguration().createCompatibleImage(distWidth, distHeight, Transparency.TRANSLUCENT);
        DrawOperate.draw(res.getGraphics(), (g)-> g.drawImage(ori, 0,0,distWidth, distHeight,oriLeft,oriTop,oriWidth,oriHeight, null));
        return res;
    }

    /**
     * 调整大小.
     * @param ori 原始图片.
     * @param width 新宽度.
     * @param height 新高度.
     * @return 调整后的图片.
     */
    public static BufferedImage resize(BufferedImage ori, int width, int height){
        return scale(ori, width, height, 0,0,ori.getWidth(), ori.getHeight());
    }

}
